package com.fanbai.score.business.http;

import android.content.Context;

import com.fanbai.score.common.Utils.JsonUtils;
import com.fanbai.score.common.http.CallBack;
import com.fanbai.score.common.http.HttpUtils;
import com.fanbai.score.common.http.WebCodes;
import com.fanbai.score.user.bean.HeadImage_Bean;
import com.fanbai.score.user.bean.User_Bean;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.HashMap;
import java.util.List;

/**
 * Created by acer on 2017/6/18.
 */

public class Busin_User_RequestNet {

    /**
     * 获取个人信息
     * @param ctx
     * @return
     */
    public static void login(final Context ctx, String mobile, String passwd, final CallBack<User_Bean> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "login");
        params.put("mobile", mobile);
        params.put("passwd", passwd);
        String url = WebCodes.HTTP_URL_BUSIN_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    User_Bean bean = JsonUtils.toBean(body, User_Bean.class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, bean);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }


    /**
     * 获取首页头部信息
     * @param ctx
     * @param callBack
     */
    public static void getHeadImages1(final Context ctx, final CallBack<List<HeadImage_Bean>> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getheadimages");
        String url = WebCodes.HTTP_URL_BUSIN_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    List<HeadImage_Bean> beans = JsonUtils.toArr(body, HeadImage_Bean[].class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, beans);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }
    public static void getHeadImages2(final Context ctx, final CallBack<List<HeadImage_Bean>> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "getheadimages");
        params.put("subtype", "shoplist");
        String url = WebCodes.HTTP_URL_BUSIN_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    List<HeadImage_Bean> beans = JsonUtils.toArr(body, HeadImage_Bean[].class);
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, beans);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }

    /**
     * 修改店员信息
     * @param ctx
     * @param callBack
     */
    public static void editclerkinfo(final Context ctx, String smuid, String smpersonname, String smmobile,
                                     String smdesc, final CallBack<String> callBack) {
        HashMap<String, String> params = new HashMap<String, String>();
        params.put("type", "editclerkinfo");
        params.put("smuid", smuid);
        params.put("smpersonname", smpersonname);
        params.put("smmobile", smmobile);
        params.put("smdesc", smdesc);
        String url = WebCodes.HTTP_URL_BUSIN_USER ;
        HttpUtils.post(ctx, url, params, new HttpUtils.BeanCallBack() {
            @Override
            public void onSuccess(String ret, String msg, String body) {
                try {
                    if (callBack != null)
                        callBack.onSuccess(ret, msg, body);
                } catch (Exception e) {
                    e.printStackTrace();
                    if (callBack != null)
                        callBack.onFailure(ret, "未知类型异常", null);
                    CrashReport.postCatchedException(e);
                }
            }

            @Override
            public void onFailure(String ret, String msg, String body) {
                if (callBack != null) {
                    callBack.onFailure(ret, msg, body);
                }
            }

            @Override
            public void onError(String code, Exception e) {
                if (callBack != null)
                    callBack.onError(code, e.getMessage());
            }
        });
    }
}
